package shopping.demo.controller.exception;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import shopping.demo.code.ResponseCode;
import shopping.demo.entities.response.Response;
import shopping.demo.exception.*;
import shopping.demo.exception.goods.GoodAlreadyExistException;
import shopping.demo.exception.goods.GoodCountNotEnoughException;
import shopping.demo.exception.goods.GoodNotExistException;
import shopping.demo.exception.goods.GoodNotValidException;
import shopping.demo.exception.order.OrderAlreadyExistException;
import shopping.demo.exception.order.OrderNotExistException;
import shopping.demo.exception.order.OrderStatusOperationException;
import shopping.demo.exception.token.TokenNotExistException;
import shopping.demo.exception.token.TokenNotValidException;
import shopping.demo.exception.user.UserAlreadyExistException;
import shopping.demo.exception.user.UserMoneyNotEnoughException;
import shopping.demo.exception.user.UserNotExistException;
import shopping.demo.exception.user.UserNotValidException;

@RestControllerAdvice
@Slf4j
public class ExceptionController {
    @ExceptionHandler({NotValidException.class})
    public String notValid(NotValidException e) {
        log.error("不合法，具体是：{}",e.getLocalizedMessage());
        if(e instanceof GoodNotValidException) {
            return JSONObject.toJSONString(new Response(ResponseCode.GOODS_NOTVALID.getCode(),ResponseCode.GOODS_NOTVALID.getMsg()));
        }
        if(e instanceof UserNotValidException) {
        }
        if(e instanceof TokenNotValidException) {
            return JSONObject.toJSONString(new Response(ResponseCode.TOKEN_NOTVALID.getCode(),ResponseCode.TOKEN_NOTVALID.getMsg()));
        }

        return unknown();
    }

    @ExceptionHandler({NotExistException.class})
    public String notExist(NotExistException e) {
        log.error("不存在，具体是：{}",e.getLocalizedMessage());
        if(e instanceof GoodNotExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.GOODS_NOTEXIST.getCode(),ResponseCode.GOODS_NOTEXIST.getMsg()));
        }
        if(e instanceof UserNotExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.USER_NOTEXIST.getCode(),ResponseCode.USER_NOTEXIST.getMsg()));
        }
        if(e instanceof OrderNotExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.ORDER_DELETE_FAIL.getCode(),ResponseCode.ORDER_DELETE_FAIL.getMsg()));
        }
        if(e instanceof TokenNotExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.TOKEN_NOTEXIST.getCode(),ResponseCode.TOKEN_NOTEXIST.getMsg()));
        }
        return unknown();
    }

    @ExceptionHandler(AlreadyExistException.class)
    public String alreadyExist(AlreadyExistException e) {
        log.error("已存在，具体是：{}",e.getLocalizedMessage());
        if(e instanceof UserAlreadyExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.USER_EXIST.getCode(),ResponseCode.USER_EXIST.getMsg()));
        }
        if(e instanceof GoodAlreadyExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.GOODS_EXIST.getCode(),ResponseCode.GOODS_EXIST.getMsg()));
        }
        if(e instanceof OrderAlreadyExistException) {
            return JSONObject.toJSONString(new Response(ResponseCode.ORDER_EXIST.getCode(),ResponseCode.ORDER_EXIST.getMsg()));
        }
        return unknown();
    }

    @ExceptionHandler({NotEnoughException.class})
    public String notEnough(NotEnoughException e) {
        log.error("剩余不足，具体是：{}",e.getLocalizedMessage());
        if(e instanceof GoodCountNotEnoughException) {
            return JSONObject.toJSONString(new Response(ResponseCode.ORDER_COMMIT_FAIL_COUNT.getCode(),ResponseCode.ORDER_COMMIT_FAIL_COUNT.getMsg()));
        }

        if(e instanceof UserMoneyNotEnoughException) {
            return JSONObject.toJSONString(new Response(ResponseCode.ORDER_COMMIT_FAIL_MONEY.getCode(),ResponseCode.ORDER_COMMIT_FAIL_MONEY.getMsg()));
        }

        return unknown();
    }

    @ExceptionHandler(OperationException.class)
    public String status(Exception e) {
        if(e instanceof OrderStatusOperationException) {
            return JSONObject.toJSONString(new Response(ResponseCode.ORDER_COMMIT_FAIL_STATUS.getCode(),ResponseCode.ORDER_COMMIT_FAIL_STATUS.getMsg()));
        }

        return unknown();
    }

    public String unknown() {
        return JSONObject.toJSONString(new Response(ResponseCode.UNKNOWN.getCode(),ResponseCode.UNKNOWN.getMsg()));
    }
}
